﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Api.BLL;
using Api.Common;
using Api.Model;
using Api.Web.helper;

namespace Api.Web.Areas.Admin.Controllers
{
    public class ProjectController : AdminController
    {
        public ActionResult List()
        {
            var list = ProjectBLL.Current.GetListNoTrack(s => s.UserId == userId);
            if (list == null)
            {
                return View("_500");
            }
            return View(list);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Edit([Bind(Include = "Id,Name")] Project project)
        {
            bool flag = true;
            if (!string.IsNullOrEmpty(project.Name))
            {
                if (project.Id > 0)
                {
                    try
                    {
                        ProjectBLL.Current.UpdateIgnore(s => new { s.CreateDate, s.UserId }, project);
                    }
                    catch
                    {
                        flag = false;
                    }
                }
                else
                {
                    project.UserId = userId;
                    project.CreateDate = DateTime.Now;
                    try
                    {
                        ProjectBLL.Current.Add(project);
                    }
                    catch
                    {
                        flag = false;
                    }
                }

                if (flag)
                {
                    AjaxMsgHelper.ResponseSuccess("", null, Url.Action("List", new { Area = "Admin" }));
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗，請重試");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請填寫名稱");
            }
        }
        public ActionResult Create()
        {
            return View();
        }
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                ViewBag.Error = "參數錯誤";
                return View("Create");
            }
            Project project = ProjectBLL.Current.GetModel(s => s.Id == id && s.UserId == userId);
            if (project == null)
            {
                ViewBag.Error = "數據不存在";
                return View("Create");
            }
            return View("Create", project);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Delete(params int[] ids)
        {
            if (ids.Length > 0)
            {
                var list = ProjectBLL.Current.GetList(s => ids.Contains(s.Id) && s.UserId == userId);
                if (list == null)
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                    return;
                }
                bool result = true;
                try
                {
                    ProjectBLL.Current.DeleteList(list);
                }
                catch
                {
                    result = false;
                }
                if (result)
                {
                    AjaxMsgHelper.ResponseSuccess("刪除成功");
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請選擇一項");
            }
        }

        public ActionResult ProjectLeftMenu()
        {
            var list = ProjectBLL.Current.GetListNoTrack(s => s.UserId == userId) ?? new List<Project>();
            return PartialView("_ProjectList", list);
        }
    }
}

